<?php
Yii::import('application.widgets.chart.AbstractPieChart');

class CustomerAgeChart extends AbstractPieChart{

	public $title='Customers by Age';

	protected function getData(){
		$criteria=new CDbCriteria();
		$criteria->select='FLOOR(TIMESTAMPDIFF(YEAR, dateOfBirth, CURDATE())/5) AS calcAge , count(*) as count';

		$criteria->group='calcAge';
		$criteria->addNotInCondition('dateOfBirth', array(null));
		$commandBuilder= Yii::app()->db->getSchema()->getCommandBuilder();
		$command=$commandBuilder->createFindCommand(Customer::model()->getTableSchema(),$criteria);
		$row=$command->queryAll();
		$result=array();
		foreach ($row as $teamRow){
			$result[]=array('value'=>intval($teamRow['count']),'name'=>sprintf('%s - %s',$teamRow['calcAge']*5,$teamRow['calcAge']*5+4));
		}
		return $result;
	}
}